package com.ad.pet.app.mall;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.mall.adapter.GoodRecordAdapter;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.CommList;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.cart.Good;
import com.ad.pet.models.remote.mall.ProductDetail;
import com.ad.pet.models.remote.mall.Rating;
import com.ad.pet.models.remote.mall.Record;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by SEELE on 2019/5/14.
 */

public class GoodRecordListActivity extends AppToolbarActivity implements MallContracts.View
{
    @BindView(R.id.recycler_rating)
    RecyclerView mRecyclerRecord;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefresh;


    int page=1;
    private GoodRecordAdapter mGoodRecordAdapter;
    private String mPid;
    @Inject
    MallPresenter mMallPresenter;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mToolbar.setTitle(getString(R.string.trans_record));
    }

    @Override
    protected int contentLayout() {
        return R.layout.acitivity_good_recordlist;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mStatusLayoutManager.showContent();
        mMallPresenter.onCreate(this);
        mPid = getIntent().getStringExtra(Constant.PRODUCT_ID);
        mGoodRecordAdapter = new GoodRecordAdapter(null);
        mRecyclerRecord.setLayoutManager(new LinearLayoutManager(mActivity));
        mRecyclerRecord.setAdapter(mGoodRecordAdapter);
    }

    @Override
    protected void initData() {
        mMallPresenter.getSaleRecord(mPid,String.valueOf(page),String.valueOf(10));
    }

    @Override
    protected void initEvents() {
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mGoodRecordAdapter.setEnableLoadMore(false);
                mMallPresenter.getSaleRecord(mPid,String.valueOf(page),String.valueOf(10));

            }
        });
        mGoodRecordAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {

                mMallPresenter.getSaleRecord(mPid,String.valueOf(++page),String.valueOf(10));


            }
        }, mRecyclerRecord);



    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);

    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void getGoodDetailSuceess(ProductDetail productDetail) {

    }

    @Override
    public void getRateListSuccess(List<Rating> ratings) {

    }

    @Override
    public void getSaleRecordSuccess(List<Record> records) {
        mSwipeRefresh.setRefreshing(false);
        if (page == 1) {
            mGoodRecordAdapter.setNewData(records);
        } else {
            mGoodRecordAdapter.addData(records);
            mGoodRecordAdapter.loadMoreComplete();
        }
        if (records.size() <5) {//CommonConstant.PAGE_COUNT
            //暂无更多数据
            mGoodRecordAdapter.loadMoreEnd();
        }
        if (records.size() == 0 && page == 1) {
            mGoodRecordAdapter.setEmptyView(R.layout.layout_empty);
        }
    }

    @Override
    public void addToCartSuccess(User user) {

    }



    @Override
    public void getFlashListSuccess(CommList<List<Good>> commList) {

    }


}
